;(function($){
    $.extend({
        Oa:{
            site_url : 'http://localhost.tp5.com',//默认跳转域名
            doHform : function(dom,backurl,close,callback){//前端异步提交表单
                type = $(dom).attr("method");
                url = $(dom).attr("action");
                data = $(dom).serialize();
                var close = arguments[2] ? arguments[2] : false; //用于判断是否关闭弹出层
                $.ajax({
                    type: type,
                    url: url,
                    data: data,
                    dataType:"json",
                    beforeSend: function(request){
                        request.setRequestHeader("token", $(dom).find("input[name='__token__']").val());
                        load_index = parent.layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
                    },
                    success: function(data){
                        parent.layer.close(load_index);
                        if(data.header && data.header.token){
                            $(dom).find("input[name='__token__']").val(data.header.token);
                        }
                        if(data.code != 200){
                            layer.msg(data.msg);
                        } else {
                            if(close){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                            if(data.url) backurl = data.url;
                            if(backurl){
                                window.location.href = backurl;
                            }else if(callback){
                                callback(data); //回调方法
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    },
                    error: function(data){
                        layer.msg(data.responseText);
                    }
                });
            },
            status :function (dom,type,eq = 0) { //更新状态 启用|禁用
                var url = $(dom).attr('data-url');
                var id = $(dom).attr('data-id');
                var value = $(dom).attr('data-value');
                layer.confirm('确定要执行吗？',function(index){
                    $.post(url, {'id':id,'field':'status','value':value}, function (result) {
                        if (200 == result.code) {
                            switch (type){
                                case 'string':
                                    if(result.data.status == 1){
                                        $(dom).html('禁用');
                                        $(dom).attr('data-value',2);
                                        $(dom).parent().prevAll().eq(eq).html('<span class="label label-primary">启用</span>');
                                        if(url.indexOf('resume')){
                                            $(dom).attr('data-url',url.replace('resume','forbid'));
                                        }
                                    }else{
                                        $(dom).html('启用');
                                        $(dom).attr('data-value',1);
                                        $(dom).parent().prevAll().eq(eq).html('<span class="label label-danger">禁用</span>');
                                        if(url.indexOf('forbid')){
                                            $(dom).attr('data-url',url.replace('forbid','resume'));
                                        }
                                    }
                                    break;
                                default:
                                    if(result.data.status == 1){
                                        $(dom).html('禁用');
                                        $(dom).attr('data-value',2);
                                        $(dom).attr('src',RootPath+'img/yes.gif');
                                    }else{
                                        $(dom).html('启用');
                                        $(dom).attr('data-value',1);
                                        $(dom).attr('src',RootPath+'img/no.gif');
                                    }
                                    break;
                            }
                        }else{
                            layer.msg(result.msg);
                        }
                    }, 'json');
                    layer.close(index);
                })
            },
            delete : function(dom,callback,bootstrapTable = true){ //单个删除
                url = $(dom).attr("data-href");
                id = $(dom).attr('data-id');
                layer.confirm('确认要删除吗？',function(){
                    $.ajax({
                        type: 'post',
                        url: url,
                        data: {'id':id,'field':'delete'},
                        dataType:"json",
                        beforeSend: function(){
                            load_index = parent.layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
                        },
                        success: function(data){
                            parent.layer.close(load_index);
                            if(200 == data.code) {
                                if(callback){
                                    callback(data); //回调方法
                                }else{
                                    if(bootstrapTable){ //bootstrapTable判断表格是否是bootstrapTable插件生成的
                                        $(dom).parents('table').bootstrapTable('remove', {field: 'id', values: [id]});
                                    }else{
                                        $(dom).parents('tr').remove();
                                    }
                                }
                            }
                            layer.msg(data.msg);
                        },
                        error: function(data){
                            layer.msg(data.responseText);
                        }
                    });
                })
            },
            href : function(url){//跳转链接
                url ? window.location.href = url : location.reload();
            },
            confirm: function(dom){ //confirm 提示框
                var confirm = $(dom).attr('data-confirm');
                parent.layer.confirm(confirm, {
                    btn: ['确认','取消'], //按钮
                    shade: false //不显示遮罩
                }, function(index){
                    parent.layer.close(index);
                    var index1 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index1);
                });
            },
            reload: function(){ //刷新当前页面
                window.location.reload();
            },
            iframeIndex(iframeName){ //获取当前iframe的下标
                var n = 0;
                $.each(parent.$('iframe'),function(i,ob){
                    if(ob.name == iframeName){
                        n = i;
                        return false;
                    }
                })
                return n;
            },
            updateField: function(url,id,field,value){ //单个更新值
                $.post(url, {id:id,field:field,value:value}, function (ret) {
                    if(ret.code == 200){
                        layer.msg(ret.msg);
                    }else{
                        layer.msg(ret.msg);
                    }
                })
            },
            simpleupload: function(_this,postData){ //单图片异步上传
                var formData = new FormData();
                formData.append('file', _this.files[0]);
                formData.append('token',postData.token);
                formData.append('md5',postData.md5);
                $.ajax({
                    type: "POST",
                    url: postData.url,
                    processData: false,  // 不要处理发送的数据
                    contentType: false,   // 不要设置Content-Type请求头
                    data: formData,
                    dataType: 'json',
                    success: function (data) {
                        if (data.status == 1) {
                            alert(data.message);

                        } else {
                            alert(data.message);
                        }
                    }
                });
            },
            getThumbChart: function(){//图片数据分析图表
                $.Oa.doHform('#thumbChartData',false,false,function(data){
                    var myChart = echarts.init(document.getElementById('thumbData'));
                    var x_data = data.x_data;
                    var download_data = data.download_data;
                    var views_data = data.view_data;
                    var upload_data = data.upload_data;
                    option = {
                        title: {
                            text: '数据分析',
                            subtext: '图片素材'
                        },
                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            data:['下载','浏览','上传']
                        },
                        toolbox: {
                            show: true,
                            feature: {
                                dataZoom: {
                                    yAxisIndex: 'none'
                                },
                                dataView: {readOnly: false},
                                magicType: {type: ['line', 'bar']},
                                restore: {},
                                saveAsImage: {}
                            }
                        },
                        xAxis:  {
                            type: 'category',
                            boundaryGap: false,
                            data: x_data
                        },
                        yAxis: {
                            type: 'value',
                            axisLabel: {
                                formatter: '{value}'
                            }
                        },
                        series: [
                            {
                                name:'下载',
                                type:'line',
                                data:download_data,
                                markPoint: {
                                    data: [
                                        {type: 'max', name: '最大值'},
                                        {type: 'min', name: '最小值'}
                                    ]
                                },
                                markLine: {
                                    data: [
                                        {type: 'average', name: '平均值'}
                                    ]
                                }
                            },
                            {
                                name:'浏览',
                                type:'line',
                                data:views_data,
                                markPoint: {
                                    data: [
                                        {type: 'max', name: '最大值'},
                                        {type: 'min', name: '最小值'}
                                    ]
                                },
                                markLine: {
                                    data: [
                                        {type: 'average', name: '平均值'},
                                        [{
                                            symbol: 'none',
                                            x: '90%',
                                            yAxis: 'max'
                                        }, {
                                            symbol: 'circle',
                                            label: {
                                                normal: {
                                                    position: 'start',
                                                    formatter: '最大值'
                                                }
                                            },
                                            type: 'max',
                                            name: '最高点'
                                        }]
                                    ]
                                }
                            },
                            {
                                name:'上传',
                                type:'line',
                                data:upload_data,
                                markPoint: {
                                    data: [
                                        {type: 'max', name: '最大值'},
                                        {type: 'min', name: '最小值'}
                                    ]
                                },
                                markLine: {
                                    data: [
                                        {type: 'average', name: '平均值'}
                                    ]
                                }
                            }
                        ]
                    };
                    myChart.setOption(option);
                });
            },
            copayUrl: function(id,type=null){//复制链接(对内部链接)
                switch (type){
                    case 1 : //文章
                        var url = 'news/news?pageID='+id;
                        break;
                    case 2 : //商品
                        var url = '/pages/product/product?pid='+id;
                    default:
                        url = id; //默认是链接地址
                }
                var oInput = document.createElement('input');
                oInput.value = url;
                document.body.appendChild(oInput);
                oInput.select();
                document.execCommand("Copy");
                oInput.style.display = 'none';
                layer.msg("链接已复制");
            },
        }
    })



})(jQuery);